<?php
  require('objects.class.php');
  //Doi tuong BBTree duoc thua ke tu doi tuong Objects
	class DBTree extends Objects {
		var $table = ""; //Ten bang chua du lieu
		var $field_id = ""; //Truong ID cua bang
		var $field_ref = ""; //Truong quan he cap cha cua bang
		var $field_name = ""; //Truong chua thong tin tieu de cau bang
		var $field_sort = ""; //Truong sap xep cua bang
		var $cond = ""; //Dieu kien yeu cau
		var $root = 0; //Chua vi tri root cua nhanh Cay
		var $url = ""; //Chua cua nhanh cay
		var $lang = ""; //Ngon ngu trinh bay
		var $level = ""; //Cap cho phep the hien
		var $start = 1; //Vi tri the hien cua Cay
		var $objname = array(); //Danh sach thong tin cua mot nhanh cay
		
		//Constructor DBTree
		function DBTree() {
		}
		//Ham huy danh sach thong tin cua mot nhanh cay
		function destroy() {
			$this->objname = array();
		}
		//Dat cac thong tin ve bang ma ban can lay thong tin voi cac bien tuong ung
		//$table: Bang chua du lieu
		//$field_id: Truong chua ID cua bang
		//$field_ref: Truong quan he cha con neu co, khong co thi truyen vao null
		//$field_name: Truong lay thong tin lam nhan
		//$field_sort: Truong sap xep thu tu trong bang neu co, khong co xin truyen vao null
		//$cond: Dieu kien loc khi truy van du lieu
		function setDB($table, $field_id, $field_ref, $field_name, $field_sort, $cond) {
			$this->table = $table;
			$this->field_id = $field_id;
			$this->field_ref = $field_ref;
			$this->field_name = $field_name;
			$this->field_sort = $field_sort;
			$this->cond = $cond;
		}
		//Dat vi tri Root cua nhanh cay
		function setRoot($root) {
			$this->root = $root;
		}
		//Dat vi tri the hien cua nhanh cay
		function setStart($start) {
			$this->start = $start;
		}
		//Dat link tuong ung cho nhanh cay
		function setUrl($url) {
			$this->url = $url;
		}
		//Dat cap cho phep the hien
		function setLevel($level) {
			$this->level = $level;
		}
		//Dat ngon ngu trinh bay
		function setLang($lang) {
			$this->lang = $lang;
		}
		//Goi thuc hien lay thong tin tren bang
		function commit() {
			global $dbi;
			if($this->field_ref == "")
				$this->root = 0;
			$ksub = $this->root;
			$break = true;
			$stack = Array();
			$pt = 0;
			$run = $this->start;
			$point = 0;
			$flag = 1;
			while($break) {
				if($ksub != 0 && $flag) //Lay ca nut goc
					$sql = "select * from " . $this->table . " where " . $this->field_id . "='" . $ksub . "' " . $this->cond;
				else {
					if($this->field_ref != "") //Co quan he cha con
						$sql = "select * from " . $this->table . " where " . $this->field_ref . "='" . $ksub . "' " . $this->cond;
					else //Khong co quan he cha con
						$sql = "select * from " . $this->table . " where 1 " . $this->cond;
					if($this->field_sort != "") //Co sap xep
						$sql .= " order by " . $this->field_sort . " asc";
				}
				$flag = 0;
				$ret = sql_query($sql, $dbi);
				if($ret)
					while($obj = sql_fetch_object($ret, 1)) {
						$stack[$point] = new Objects;
						$field = $this->field_id;
						$stack[$point]->setId($obj->$field);
						$field = $this->field_name;
						$stack[$point]->setName($obj->$field);
						$stack[$point]->setStep($run);
						$point++;
					}
				$exits = true;
				if($point != 0)
					do {
						$pop = $stack[$point - 1]->getId();
						$run = $stack[$point - 1]->getStep();
						$this->objname[$pt] = new Objects;
						$this->objname[$pt]->setObj($stack[$point - 1]);
						$pt++;
						$point = $point - 1;
						if($point <= 0)
							$exits = false;
						$check = false;
						if($this->field_ref != "") {
							$tmp = " and " . $this->field_ref . "='" . $pop . "' " . $this->cond . " ";
							$check = check_empty($this->table, $tmp);
							if($run >= $this->level) {
								$check = false;
								if($exits == false)
									$break = false;
							}
						} else
							if($exits == false)
								$break = false;
					} while (!$check && $exits != false);
				else
					$break = false;
				$run++;
				$ksub = $pop;
			}
		}
		//Ham nay tao mot Array chua danh sach cua mot nhanh cay va tra ve Array sau khi tao
		function getTree() {
			$this->commit();
			$count = count($this->objname);
			$mn = array();
			for($i = 0; $i < $count; $i++) {
				$k = 0;
				$end = "smLine";
				$cs = $this->objname[$i]->getStep();
				if((($i + 1) == $count || $cs > $this->objname[$i + 1]->getStep()) && $cs > 1)
					$end = "smLineEnd";
				$mn[$i][$k++] = $this->objname[$i]->getName();
				$mn[$i][$k++] = $this->url;
				$mn[$i][$k++] = $cs;
				$mn[$i][$k++] = $end;
				if($this->objname[$i]->getId() != $this->root)
					$mn[$i][$k++] = $this->objname[$i]->getId();
				else
					$mn[$i][$k++] = "";
			}
			return $mn;
		}
	}
?>
